package cn.stronglink.asset.message.mq.dam.handle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import cn.stronglink.asset.message.mq.dam.entity.IScreenCommandFactory;
import cn.stronglink.asset.message.mq.dam.entity.RackCodeEntity;
import cn.stronglink.asset.message.mq.dam.entity.ScreenCommandEntity;
import cn.stronglink.asset.message.mq.dam.entity.dacai.UpdateArrayTextCommandFactory;
import cn.stronglink.asset.message.mq.dam.message.MQMessageOfDAM;
import cn.stronglink.asset.message.mq.topic.ReaderInfoSender;
import cn.stronglink.asset.module.dam.service.DamService;
import cn.stronglink.asset.module.dam.service.IDamService;
import cn.stronglink.asset.terminal.bjc.model.AssetModelVO;
import cn.stronglink.suite.core.util.ContextUtils;

public class OpenSetModelUHeightPageHandle extends AbstractScreenCommandHandle{
	protected final static String TOPIC_NAME = "serviceToDam";
	protected final static String ACTION_CODE = "screenRequest";
	protected final static String SEND_ACTION_CODE= "screenCommand";
	protected IDamService damService =(DamService)ContextUtils.getBean("sensorService");
	private ReaderInfoSender topicSender = (ReaderInfoSender) ContextUtils.getBean("readerInfoSender");
	private static final int SCREEN_MAX_SHOW_NUM = 16; 
	private static final short SCREEN_ID = 13;
	
	@Override
	protected String getCommandCode() {
		return "EEB11100070003100000";
	}

	@Override
	protected void handleMessage(MQMessageOfDAM msg) {
		try {
		RackCodeEntity entity = (RackCodeEntity) msg.getAwsPostdata();
		if(entity!=null) {
			List<AssetModelVO> modelList = damService.qryAssetModelListByRackConverCode(entity.getRackConverCode());
			Map<Short, String> map = new HashMap<>();
			for(int i=0;i<SCREEN_MAX_SHOW_NUM;i++) {
				if(modelList!=null) {
				  if(i>modelList.size()-1) {
					  map.put((short)(3*i+1), "");
					  map.put((short)(3*i+2), "");
					  map.put((short)(3*i+3), "");
				  }else {
					  AssetModelVO assetModel = modelList.get(i);
					  map.put((short)(3*i+1), assetModel.getId_());
					  map.put((short)(3*i+2), assetModel.getBrandName()+"-"+assetModel.getName());
					  map.put((short)(3*i+3), assetModel.getuHeight()==null?"":String.valueOf(assetModel.getuHeight()));
				  }
				}else {
					  map.put((short)(3*i+1), "");
					  map.put((short)(3*i+2), "");
					  map.put((short)(3*i+3), "");
				}
				this.sendModelListToScreen(entity.getRackConverCode(),map);
				map.clear();
			}
		}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void sendModelListToScreen(String rackConverCode, Map<Short, String> map) throws Exception {
		IScreenCommandFactory rackScreenCommandFactroy = new UpdateArrayTextCommandFactory(rackConverCode, SCREEN_ID, map);
		ScreenCommandEntity sc =  rackScreenCommandFactroy.createScreenCommand();
		
		MQMessageOfDAM message = new MQMessageOfDAM();
		message.setActioncode(SEND_ACTION_CODE);
		message.setAwsPostdata(sc);
		message.setTimestamp(System.currentTimeMillis());
		
		String json = JSON.toJSONString(message);
		topicSender.send(TOPIC_NAME,json);
		logger.info("topic:{}  发送命令:{}",TOPIC_NAME,json);
		
	}

}
